package com.stepshop.sys.service.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stepshop.core.service.impl.BaseServiceImpl;
import com.stepshop.sys.bean.SysMsg;
import com.stepshop.sys.dao.SysMsgMapper;
import com.stepshop.sys.service.SysMsgService;
import com.stepshop.wechat.utils.HttpClientUtil;
@Service
public class SysMsgServiceImpl extends BaseServiceImpl<SysMsg, SysMsgMapper> implements SysMsgService{
	private HttpClientUtil utils=new HttpClientUtil();
	private Logger logger=Logger.getLogger(this.getClass());
	private int count=0;
	private WebApplicationContext webApplicationContext = null;  
	private ServletContext servletContext = null;
	@Override
	public void sendMsg() {
		logger.info("------------------------------------------------------------------");
		List<SysMsg> msgs = queryMsgsNotSend();
		if(msgs!=null&&(!msgs.isEmpty())){
			for(SysMsg msg:msgs){
				try {
					sendMsg(msg);
					msg.setStatus(1);
					updateById(msg);
					deleteById(msg.getId());
					logger.info("id["+msg.getId()+"]发送消息成功"+"  消息体【"+msg.getSendData()+"】");
				} catch (Exception e) {
					logger.info("id["+msg.getId()+"]发送消息异常"+e);
				}
			}
		}
	}
	/**
	 * 查询未发送的信息 
	 */
	private List<SysMsg> queryMsgsNotSend(){
		return mapper.queryMsgsByStatus(0);
	}
	/**
	 * 发送消息 
	 */
	private void sendMsg(SysMsg msg){
		String json=utils.doPost("http://wechat-pre.0071515.com/pangu-wechat/dimenCode/getToken.do?valid=iiudihfeqpxkegEl4dkiR4SKd","","UTF-8");
		JSONObject object = JSON.parseObject(json);
		String token=object.getString("token");
		String postUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
		String bodyData = msg.getSendData();
		Map<String,Object> dataMap=new HashMap<String,Object>();
		String url=""+msg.getUrl();
		String openid=msg.getOpenid();
		String templateId="";
		Integer iddd=1;
		if(iddd==1){
			templateId="i2_e1jfRyP1wbcrR-EarOCCKul6c66-acVd-JAVOl04";
		}
		dataMap.put("data",bodyData);
		dataMap.put("touser",openid);
		dataMap.put("url",url);
		dataMap.put("template_id",templateId);
		String dataJson=JSON.toJSONString(dataMap);
		String xml = utils.doPost(postUrl, dataJson, "UTF-8");
		logger.info("id["+msg.getId()+"]  xml["+xml+"]");
	}
	public void test(){
		logger.info("定时器test-------------"+count++);
		//springMVC获取当前ServletContext(当前web应用)
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
        ServletContext servletContext = webApplicationContext.getServletContext();
        try {
			URL resource = servletContext.getResource("js/ajaxfileupload.js");
			System.out.println(servletContext.getRealPath(resource.getFile()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
        System.out.println(servletContext.getContextPath());
	}
}
